<template>
  <div class="table-page-container layout-padding">
    <el-card shadow="hover" class="layout-padding-auto">
      <div class="system-user-search mb15">
        <el-button
          v-if="$can('create', { type: 'ProjectType', __type: 'Dictionary' })"
          size="default"
          type="primary"
          class="ml10"
          @click="openAddDialog()"
        >
          <el-icon>
            <ele-Plus />
          </el-icon>
          新增
        </el-button>
      </div>
      <el-table :data="tableData.list" v-loading="loading" element-loading-text="加载中" border style="width: 100%">
        <el-table-column type="index" label="序号" width="60" align="center" />
        <el-table-column prop="name" label="类型名称"></el-table-column>
        <el-table-column prop="power" label="功率" show-overflow-tooltip></el-table-column>
        <el-table-column prop="created_at" label="创建时间" show-overflow-tooltip></el-table-column>
        <el-table-column label="操作" width="100">
          <template #default="scope">
            <el-button
              v-if="$can('update', 'MachineType')"
              size="small"
              text
              type="primary"
              @click="handleEdit(scope.row)"
              >修改</el-button
            >
            <el-button
              v-if="$can('delete', 'MachineType')"
              size="small"
              text
              type="primary"
              @click="handleDelete(scope.row)"
              >删除</el-button
            >
          </template>
        </el-table-column>
      </el-table>
      <CommonPagination :pagination="pagination" :total="tableData.total"></CommonPagination>
    </el-card>
    <MachineTypeDialog ref="dialogRef" @refresh="refresh" />
  </div>
</template>

<script setup lang="ts" name="machineType">
import { defineAsyncComponent, ref } from 'vue';
import { ElMessageBox, ElMessage } from 'element-plus';
import { readAllMachineType, deleteMachineType } from '/@/api/machine-type';
import { useTable } from '/@/hooks/use-table';
import CommonPagination from '/@/components/common-pagination/index.vue';
import { MachineType } from '/@/models';
import { DialogType } from '/@/constants/dialog-type';

const MachineTypeDialog = defineAsyncComponent(() => import('./dialog.vue'));

const dialogRef = ref();

const { tableData, loading, pagination, refresh } = useTable<MachineType>(({ page, pageSize }) =>
  readAllMachineType({ page, pageSize }).then(({ data }) => {
    return {
      total: data?.total,
      list: data?.list,
    };
  })
);

const openAddDialog = () => {
  dialogRef.value.openDialog(DialogType.CREATE);
};

const handleEdit = (row: MachineType) => {
  dialogRef.value.openDialog(DialogType.EDIT, row);
};

const handleDelete = (row: MachineType) => {
  ElMessageBox.confirm(`此操作不可恢复，是否继续?`, '提示', {
    confirmButtonText: '确认',
    cancelButtonText: '取消',
    type: 'warning',
    buttonSize: 'default',
    beforeClose: async (action, instance, done) => {
      if (action === 'confirm') {
        instance.confirmButtonLoading = true;
        instance.confirmButtonText = '提交中';
        await deleteMachineType(row.id).then(() => {
          ElMessage.success('删除成功');
          refresh();
        });

        instance.confirmButtonLoading = false;
        done();
      } else {
        done();
      }
    },
  });
};
</script>
